package com.blockchain.front.servlets.financial;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.config.ConfigureProvider;
import com.blockchain.framework.http.servlet.annotation.PagingServlet;
import com.blockchain.framework.http.session.Session;
import com.blockchain.framework.http.session.authentication.AuthenticationException;
import com.blockchain.framework.http.session.authentication.VerifyCodeAuthentication;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.resource.ResourceRegister;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.front.service.financial.FinancialManage;
import com.blockchain.front.service.security.SecurityManage;
import com.blockchain.util.parser.BigDecimalParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.variables.defines.SystemVariable;
@PagingServlet(itemServlet = CnyTx.class)
public class CnyTx extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		BigDecimal txje=BigDecimalParser.parse(request.getParameter("txje"));
		String jymm=request.getParameter("tjmx");
		int dzid=IntegerParser.parse(request.getParameter("dzid"));
		/**获取汇率*/		
		final ConfigureProvider configureProvider = ResourceRegister.getResourceProvider(getServletContext())
				.getResource(ConfigureProvider.class);
		BigDecimal rate = new BigDecimal(configureProvider.get("SYSTEM.EUR_CNY_RATE"));

		BigDecimal fv = new BigDecimal(configureProvider.getProperty(SystemVariable.WITHDRAW_FV));

		BigDecimal sxf = txje.multiply(fv).setScale(8, BigDecimal.ROUND_HALF_UP);
		
		FinancialManage manage=serviceSession.getService(FinancialManage.class);
		manage.withdraw(txje, jymm, dzid,rate);
		
		/**更新该银行卡的预留手机号*/
		try {
			SecurityManage sManage=serviceSession.getService(SecurityManage.class);
			sManage.updateBankCard(dzid,"UPDATE T6024 SET F11=? WHERE F01=?",request.getParameter("phoneNumber"),dzid);
			
		} catch (Exception e) {			
			e.printStackTrace();
		} 
		
		prompt(request, response, PromptLevel.INFO, "申请成功，我们将尽快为您处理！");
		sendRedirect(request, response, getController().getViewURI(request, CnyTx.class));
	}
}
